﻿@model WebApp.Models.S_role

@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}
@using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
{
    <div class="dialog-content">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <input type="hidden" id="Id" value="@Model.Id" />
        <input type="hidden" value="@(Model.Updatetime.HasValue ? Model.Updatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "")" class="form-control datafield" data-fieldname="Updatetime" />
        
        <div class="row">
            <div class="col-xs-12 nopadding">
                <div class="widget-box">
                    <div class="widget-title">
                        <h5>角色信息</h5>
                    </div>
                    <div class="widget-content">
                        <table cellpadding="3" cellspacing="0" border="0" class="inputform">
                            <tr>
                                <td class="td-label"><label for="Roleid">角色代码</label></td>
                                <td class="td-control">
                                    @Html.TextBox("Roleid", @Model.Roleid, new { @class = "form-control datafield", @data_fieldname = "Roleid" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Rolename">角色名称</label></td>
                                <td class="td-control" style="width:300px;">
                                    @Html.TextBox("Rolename", @Model.Rolename, new { @class = "form-control datafield", @data_fieldname = "Rolename" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Description">角色说明</label></td>
                                <td class="td-control">
                                    @Html.TextArea("Description", @Model.Description, new { @class = "form-control datafield", @data_fieldname = "Description" })
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="dialog-footer">
        <button id="btncancel" class="btn btn-small btn-default">取消</button>
        <button id="btnsave" class="btn btn-small btn-primary">保存</button>
    </div>
    
}
@section scripts{

<script type="text/javascript">


    $(document).ready(function () {

        initValidate();
        //保存
        $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
        //取消
        $("#btncancel").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });
    });

    //数据验证
    function initValidate() {
        //表单验证
        $('#inputform').validate({
            rules: {
                Roleid: {
                    required: true,
                    maxlength: 30,
                    remote: {
                        url: baseurl + 'Manage/Role/ExsitCheck',
                        type: 'post',
                        data: {
                            id: $("#Id").val(),
                            roleid: $("#Roleid").val()
                        },
                        dataFilter: function (data, type) {
                            if (data == "yes")
                                return true;
                            else
                                return false;
                        }
                    }
                },
                Rolename: { required: true, maxlength: 50 },
                Description: {
                    maxlength: 100
                }
            },
            errorPlacement: function (error, element) {
                var container = element.parents("div.col-sm-6:eq(0)");
                var errorContainer = container.next();
                error.appendTo(errorContainer);
            },
            messages: {
                Roleid: {
                    remote: '角色编号已经存在'
                }
            }
        });
    }

    function save(obj)
    {
        if (!$("#inputform").valid()) {
            return;
        }

        var jsondata = {
            Id: $("#Id").val(),
            Values: {}
        };
     
        //主表数据
        $("#inputform .datafield").each(function () {
            jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
        });
        //禁用保存按钮，防止重复提交
        $(obj).attr("disabled", true);

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Role/Save",
            data: {jsondata: JSON.stringify(jsondata) },
            success: function (e) {
                $(obj).attr("disabled", false);
                if (e.Ret == "0") {
                    parent.closeDialog(true);
                } else {
                    alert(e.Msg);
                }
            }
        });
    }

    </script>
}
